package com.ywy.utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 日期工具类
 */
public class DateUtil {
    public static final String PURE_DATETIME_PATTERN = "yyyyMMddHHmmss";
    public static final String SLASH_DATE_PATTERN = "yyyy/MM/dd";

    /**
     * 日期对象转字符串
     *
     * @param date
     * @param format
     * @return
     */
    public static String formatDate(Date date, String format) {
        String result = "";
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        if (date != null) {
            result = sdf.format(date);
        }
        return result;
    }

    /**
     * 日期对象转字符串
     * @return
     */
    public static String formatDatePure() {
        return formatDate(new Date(), PURE_DATETIME_PATTERN);
    }

    /**
     * 日期对象转字符串
     * @return
     */
    public static String formatDateSlash() {
        return formatDate(new Date(), SLASH_DATE_PATTERN);
    }

    /**
     * 判断当前时间距离第二天凌晨的秒数
     *
     * @return 返回值单位为[s:秒]
     */
    public static long getSecondsNextEarlyMorning() {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DAY_OF_YEAR, 1);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return (cal.getTimeInMillis() - System.currentTimeMillis()) / 1000;
    }
}
